package com.inei.evaluacionemype.controller.activity;

import com.inei.evaluacionemype.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class CapituloII_Activity extends Activity {

	// componentes
	private EditText etC2_01, etC2_02, etC2_03, etC2_04, etC2_05, etC2_06,
			etC2_07, etC2_08, etC2_09, etC2_10, etC2_11, etC2_12, etC2_13;

	// variables

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_capitulo_ii);

		IniciarComponentes();

		AgregarEventos();

		CargarDatos();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.capitulo_ii, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnGuardar:
			String resultado = Validar();
			// if (resultado.equals("")) {
			if (true) {
				GuardarDatos();
			} else {
				Toast.makeText(this, resultado, Toast.LENGTH_SHORT).show();
			}

			break;

		case R.id.mnSalir:
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Intent ir_cap1 = new Intent(this, CapituloI_Activity.class);
		ir_cap1.putExtras(getIntent().getExtras());
		startActivity(ir_cap1);
		finish();
		super.onBackPressed();
	}

	private void IniciarComponentes() {
		etC2_01 = (EditText) findViewById(R.id.etC2_01);
		etC2_02 = (EditText) findViewById(R.id.etC2_02);
		etC2_03 = (EditText) findViewById(R.id.etC2_03);
		etC2_04 = (EditText) findViewById(R.id.etC2_04);
		etC2_05 = (EditText) findViewById(R.id.etC2_05);
		etC2_06 = (EditText) findViewById(R.id.etC2_06);
		etC2_07 = (EditText) findViewById(R.id.etC2_07);
		etC2_08 = (EditText) findViewById(R.id.etC2_08);
		etC2_09 = (EditText) findViewById(R.id.etC2_09);
		etC2_10 = (EditText) findViewById(R.id.etC2_10);
		etC2_11 = (EditText) findViewById(R.id.etC2_11);
		etC2_12 = (EditText) findViewById(R.id.etC2_12);
		etC2_13 = (EditText) findViewById(R.id.etC2_13);
	}

	private void AgregarEventos() {
		EventoClave06(etC2_01);
		EventoClave06(etC2_02);
		EventoClave06(etC2_03);
		EventoClave06(etC2_04);
		EventoClave06(etC2_05);

		EventoClave13(etC2_07);
		EventoClave13(etC2_08);
		EventoClave13(etC2_09);
		EventoClave13(etC2_10);
		EventoClave13(etC2_11);
		EventoClave13(etC2_12);
	}

	private void EventoClave06(EditText et) {
		et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				double n01 = Double.parseDouble(etC2_01.getText().toString()
						.trim().length() == 0 ? "0" : etC2_01.getText()
						.toString());
				double n02 = Double.parseDouble(etC2_02.getText().toString()
						.trim().length() == 0 ? "0" : etC2_02.getText()
						.toString());
				double n03 = Double.parseDouble(etC2_03.getText().toString()
						.trim().length() == 0 ? "0" : etC2_03.getText()
						.toString());
				double n04 = Double.parseDouble(etC2_04.getText().toString()
						.trim().length() == 0 ? "0" : etC2_04.getText()
						.toString());
				double n05 = Double.parseDouble(etC2_05.getText().toString()
						.trim().length() == 0 ? "0" : etC2_05.getText()
						.toString());
				double sum06 = n01 + n02 + n03 + n04 + n05;

				etC2_06.setText((sum06) + "");
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	private void EventoClave13(EditText et) {
		et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				double n07 = Double.parseDouble(etC2_06.getText().toString()
						.trim().length() == 0 ? "0.0" : etC2_06.getText()
						.toString());
				double n08 = Double.parseDouble(etC2_08.getText().toString()
						.trim().length() == 0 ? "0.0" : etC2_08.getText()
						.toString());
				double n09 = Double.parseDouble(etC2_09.getText().toString()
						.trim().length() == 0 ? "0.0" : etC2_09.getText()
						.toString());
				double n10 = Double.parseDouble(etC2_10.getText().toString()
						.trim().length() == 0 ? "0.0" : etC2_10.getText()
						.toString());
				double n11 = Double.parseDouble(etC2_11.getText().toString()
						.trim().length() == 0 ? "0.0" : etC2_11.getText()
						.toString());
				double n12 = Double.parseDouble(etC2_12.getText().toString()
						.trim().length() == 0 ? "0.0" : etC2_12.getText()
						.toString());

				etC2_13.setText((n07 + n08 + n09 + n10 + n11 + n12) + "");
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	private void CargarDatos() {

	}

	private void GuardarDatos() {
		Intent ir_cap3 = new Intent(this, CapituloIII_Activity.class);
		startActivity(ir_cap3);
		finish();
	}

	private String Validar() {
		return "";
	}
}
